import com.mit.web.business.proxy.PurchaseProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.DataFormatUtil;
import com.mit.web.util.Validator;
import com.mit.web.view.component.list.SupplierComboBox;
import com.mit.web.view.component.list.SupplierMaterialComboBox;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.vo.purchase.InquiryItemVO;
import com.mit.web.vo.purchase.POItemVO;

import flash.events.MouseEvent;

import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.rpc.events.ResultEvent;

import spark.events.IndexChangeEvent;

[Bindable] private var _item:POItemVO;
[Bindable] private var _queryItem:InquiryItemVO;
private var _proxy:PurchaseProxy = PurchaseProxy.getInstance();
private var _pm:IProgressMonitor;
private var _logger:ILogger;

public function set item(i:POItemVO):void{
	_item = i;
}

private function loadQueryInfo(event:FlexEvent):void{
	var requiredColorId:int = 0;
	//如果要区分颜色，才将颜色作为参数传送到后面找询价记录。
	if(_item.fabric.coloredInd == Constants.YES)
		requiredColorId = _item.moItem.colorId;
	var rsp:IInvokeResponder = _proxy.getInquiryInfoByItem(_item.fabric.id, requiredColorId).installProgressBar(_pm, this.owner);
	rsp.addResultListener(retrieveQueryItemHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function retrieveQueryItemHandler(event:ResultEvent):void{
	var i:InquiryItemVO = event.result as InquiryItemVO;
	if(i == null)
		MessageUtil.popNoRecordMessage(this);
	_queryItem = i;
	
}

protected function box_supplier_changeHandler(event:IndexChangeEvent):void
{
	// If index > -1 and index changed, enable and refresh material dropdownlist
	var c:SupplierComboBox = event.currentTarget as SupplierComboBox;
	if(event.newIndex != event.oldIndex){
		box_material.supplierId = c.selectedId;
		//box_material.selectedIndex = -1;
		//box_material.textInput.text = null;
		box_material.selectedId = 0;
		//box_color.selectedIndex = -1;
		//box_color.textInput.text = null;
		box_color.selectedId = 0;
		box_color.enabled =false;
	}
}

protected function box_material_changeHandler(event:IndexChangeEvent):void
{
	var mc:SupplierMaterialComboBox = event.currentTarget as SupplierMaterialComboBox;
	if(event.newIndex != event.oldIndex){
		box_color.materialId = mc.selectedId;
		box_color.selectedId = 0;
		//box_color.selectedIndex = -1;
		//box_color.textInput.text = null;
	}
}

private function doSave(event:MouseEvent):void{
	if(validate() == false)
		return;
	
	_item.supplierId = box_supplier.selectedId;
	_item.supplierName = box_supplier.selectedItem.label;
	_item.material.id = box_material.selectedId;
	_item.materialColor.id = box_color.selectedId;
	_item.price = Number(txt_price.text);
	_item.qty = Number(txt_qty.text);
	_item.warehouse.id = lst_warehouse.selectedId;
	_item.latestDeiverDate = dat_deliver.selectedDate;
	//TODO 采购的单位 = 用量估计的单位?
	_item.unit = _item.fabric.unit;
	
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.SAVED);
	this.dispatchEvent(e);
	closeWindow(this);
	
	
}

private function validate():Boolean{
	if(box_supplier.selectedId == 0 ){
		showRequiredErrorTip(box_supplier);
		return false;
	}
	if(box_material.selectedId == 0 ){
		showRequiredErrorTip(box_material);
		return false;
	}	
	if(box_color.selectedId == 0 ){
		showRequiredErrorTip(box_color);
		return false;
	}
	if(Validator.isNullOrBlank(txt_price.text) || Number(txt_price.text) <= 0){
		showRequiredErrorTip(txt_price);
		return false;
	}
	if(Validator.isNullOrBlank(txt_qty.text) || Number(txt_qty.text) <= 0){
		showRequiredErrorTip(txt_qty);
		return false;
	}	
	if(lst_warehouse.selectedId == 0 ){
		showRequiredErrorTip(lst_warehouse);
		return false;
	}
	return true;
}
